package com.winbaoxian.testng.platform.service.helper;

import com.winbaoxian.module.security.model.dto.WinSecurityBaseUserDTO;
import com.winbaoxian.testng.model.dto.TestCasesDTO;
import com.winbaoxian.testng.platform.model.dto.*;
import com.winbaoxian.testng.platform.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author dongxuanliang252
 * @date 2019-03-25 14:01
 */
@Service
public class DataWrapHelper {

    @Resource
    private UserService userService;
    @Resource
    private ProjectService projectService;
    @Resource
    private ModuleService moduleService;
    @Resource
    private TestCasesService testCasesService;
    @Resource
    private ReportService reportService;
    @Value("${project.domain}")
    private String projectDomain;

    public void wrapActionTemplateList(List<PlatformActionTemplateDTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return;
        }
        List<Long> userIdList = dtoList.stream().filter(o -> o.getCreatorUid() != null).map(o -> o.getCreatorUid()).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }
        List<WinSecurityBaseUserDTO> userDTOList = userService.getByIdList(userIdList);
        if (CollectionUtils.isEmpty(userDTOList)) {
            return;
        }
        Map<Long, String> idNameMap = userDTOList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o.getName()));
        for (PlatformActionTemplateDTO templateDTO : dtoList) {
            if (templateDTO.getCreatorUid() != null && idNameMap.containsKey(templateDTO.getCreatorUid())) {
                templateDTO.setCreatorName(idNameMap.get(templateDTO.getCreatorUid()));
            }
        }
    }

    public void wrapTestCasesList(List<PlatformTestCasesDTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return;
        }
        Set<Long> userIdSet = new HashSet<>();
        Set<Long> projectIdSet = new HashSet<>();
        Set<Long> moduleIdSet = new HashSet<>();
        for (PlatformTestCasesDTO dto : dtoList) {
            if (dto.getCreatorUid() != null) {
                userIdSet.add(dto.getCreatorUid());
            }
            if (dto.getOwnerUid() != null) {
                userIdSet.add(dto.getOwnerUid());
            }
            if (dto.getProjectId() != null) {
                projectIdSet.add(dto.getProjectId());
            }
            if (dto.getModuleId() != null) {
                moduleIdSet.add(dto.getModuleId());
            }
        }
        Map<Long, String> idNameUserMap = null;
        Map<Long, String> idNameProjectMap = null;
        Map<Long, String> idNameModuelMap = null;
        if (CollectionUtils.isNotEmpty(userIdSet)) {
            List<WinSecurityBaseUserDTO> userDTOList = userService.getByIdList(new ArrayList<>(userIdSet));
            if (CollectionUtils.isNotEmpty(userDTOList)) {
                idNameUserMap = userDTOList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o.getName()));
            }
        }
        if (CollectionUtils.isNotEmpty(projectIdSet)) {
            List<PlatformProjectDTO> projectDTOList = projectService.getByIdList(new ArrayList<>(projectIdSet));
            if (CollectionUtils.isNotEmpty(projectDTOList)) {
                idNameProjectMap = projectDTOList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o.getName()));
            }
        }
        if (CollectionUtils.isNotEmpty(userIdSet)) {
            List<PlatformModuleDTO> moduleDTOList = moduleService.getByIdList(new ArrayList<>(moduleIdSet));
            if (CollectionUtils.isNotEmpty(moduleDTOList)) {
                idNameModuelMap = moduleDTOList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o.getName()));
            }
        }
        for (PlatformTestCasesDTO testCasesDTO : dtoList) {
            if (MapUtils.isNotEmpty(idNameUserMap)) {
                if (testCasesDTO.getCreatorUid() != null && idNameUserMap.containsKey(testCasesDTO.getCreatorUid())) {
                    testCasesDTO.setCreatorName(idNameUserMap.get(testCasesDTO.getCreatorUid()));
                }
                if (testCasesDTO.getOwnerUid() != null && idNameUserMap.containsKey(testCasesDTO.getOwnerUid())) {
                    testCasesDTO.setOwnerName(idNameUserMap.get(testCasesDTO.getOwnerUid()));
                }
            }
            if (MapUtils.isNotEmpty(idNameProjectMap)) {
                if (testCasesDTO.getProjectId() != null && idNameProjectMap.containsKey(testCasesDTO.getProjectId())) {
                    testCasesDTO.setProjectName(idNameProjectMap.get(testCasesDTO.getProjectId()));
                }
            }
            if (MapUtils.isNotEmpty(idNameModuelMap)) {
                if (testCasesDTO.getModuleId() != null && idNameModuelMap.containsKey(testCasesDTO.getModuleId())) {
                    testCasesDTO.setModuleName(idNameModuelMap.get(testCasesDTO.getModuleId()));
                }
            }
        }
    }

    public void wrapTestReportList(List<PlatformTestReportDTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return;
        }
        //用户
        List<Long> userIdList = dtoList.stream().filter(o -> o.getExecutorUid() != null).map(o -> o.getExecutorUid()).distinct().collect(Collectors.toList());
        Map<Long, String> userIdNameMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(userIdList)) {
            List<WinSecurityBaseUserDTO> userDTOList = userService.getByIdList(userIdList);
            if (CollectionUtils.isNotEmpty(userDTOList)) {
                userIdNameMap = userDTOList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o.getName()));
            }
        }
        //项目名称
        List<String> reportUuidList = dtoList.stream().map(o -> o.getReportUuid()).collect(Collectors.toList());
        Map<String, List<PlatformTestReportDetailsDTO>> reportUuidDetailsMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(reportUuidList)) {
            List<PlatformTestReportDetailsDTO> testCasesDTOList = reportService.getSimpleTestReportDetailsList(reportUuidList);
            reportUuidDetailsMap = testCasesDTOList.stream().collect(Collectors.groupingBy(o -> o.getReportUuid()));
        }
        for (PlatformTestReportDTO reportDTO : dtoList) {
            //用户名称
            if (MapUtils.isNotEmpty(userIdNameMap) && reportDTO.getExecutorUid() != null && userIdNameMap.containsKey(reportDTO.getExecutorUid())) {
                reportDTO.setExecutorName(userIdNameMap.get(reportDTO.getExecutorUid()));
            }
            //项目名称、测试任务名称
            if (MapUtils.isNotEmpty(reportUuidDetailsMap) && reportDTO.getReportUuid() != null && reportUuidDetailsMap.containsKey(reportDTO.getReportUuid())) {
                List<PlatformTestReportDetailsDTO> testReportDetailsDTOS = reportUuidDetailsMap.get(reportDTO.getReportUuid());
                if (CollectionUtils.isNotEmpty(testReportDetailsDTOS)) {
                    List<String> projectNameList = testReportDetailsDTOS.stream().map(o -> o.getProjectName()).distinct().collect(Collectors.toList());
                    List<String> testCasesNameList = testReportDetailsDTOS.stream().map(o -> o.getTestCasesName()).distinct().collect(Collectors.toList());
                    if (projectNameList.size() == 1) {
                        reportDTO.setProjectNames(projectNameList.get(0));
                    } else {
                        reportDTO.setProjectNames(String.format("%s...(%s)", projectNameList.get(0), projectNameList.size()));
                    }
                    if (testCasesNameList.size() == 1) {
                        reportDTO.setTestCasesNames(testCasesNameList.get(0));
                    } else {
                        reportDTO.setTestCasesNames(String.format("%s...(%s)", testCasesNameList.get(0), testCasesNameList.size()));
                    }
                }
            }
            //测试报告地址
            reportDTO.setReportUrl(projectDomain + "/view/report/summary/" + reportDTO.getReportUuid());
        }
    }

    public void wrapTestReportDetailsList(List<PlatformTestReportDetailsDTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)) {
            return;
        }
        List<Long> idList = dtoList.stream().filter(o -> o.getTestCasesId() != null).map(o -> o.getTestCasesId()).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(idList)) {
            return;
        }
        List<PlatformTestCasesDTO> testCasesDTOList = testCasesService.getByIdList(idList);
        if (CollectionUtils.isEmpty(testCasesDTOList)) {
            return;
        }
        wrapTestCasesList(testCasesDTOList);
        Map<Long, PlatformTestCasesDTO> idNameMap = testCasesDTOList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o));
        for (PlatformTestReportDetailsDTO detailsDTO : dtoList) {
            if (detailsDTO.getTestCasesId() != null && idNameMap.containsKey(detailsDTO.getTestCasesId())) {
                PlatformTestCasesDTO testCasesDTO = idNameMap.get(detailsDTO.getTestCasesId());
                detailsDTO.setTestCasesName(testCasesDTO.getName());
                detailsDTO.setModuleName(testCasesDTO.getModuleName());
                detailsDTO.setProjectName(testCasesDTO.getProjectName());
            }
        }
    }

    public void wrapTestReportDetails(PlatformTestReportDetailsDTO dto) {
        if (dto == null || dto.getTestCasesId() == null) {
            return;
        }
        PlatformTestCasesDTO testCasesDTO = testCasesService.getById(dto.getTestCasesId());
        if (testCasesDTO == null) {
            return;
        }
        if (testCasesDTO.getModuleId() != null) {
            PlatformModuleDTO moduleDTO = moduleService.getById(testCasesDTO.getModuleId());
            if (moduleDTO != null) {
                dto.setModuleName(moduleDTO.getName());
            }
        }
        if (testCasesDTO.getProjectId() != null) {
            PlatformProjectDTO projectDTO = projectService.getById(testCasesDTO.getProjectId());
            if (projectDTO != null) {
                dto.setProjectName(projectDTO.getName());
            }
        }
        dto.setTestCasesName(testCasesDTO.getName());
    }
}
